<template>
  <div class="login">
    <div
      class="container"
      v-loading="loading"
      element-loading-text="拼命登陆中"
    >
      <div class="title">登录</div>
      <div class="con">
        <div class="userInfo">
          <span>电话: </span
          ><input
            type="text"
            v-model="userInfo.phone"
            @input="checkPhone(userInfo.phone)"
          />
          <p class="checkMassege" v-if="!mFlag">电话号码格式不对</p>
        </div>
        <div class="userInfo">
          <span>密码: </span
          ><input
            type="password"
            v-model="userInfo.password"
            @input="checkPassword(userInfo.password)"
          />
          <p class="checkMassege" v-if="!pFlag">密码格式不对</p>
        </div>
        <div class="operate">
          <button @click="userLogin(userInfo)">提交</button>
          <button @click="goRigister">注册</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { userLogin } from "../assets/request.js";
export default {
  name: "Login",
  data() {
    return {
      loading: false,
      userInfo: {},
      mFlag: true,
      pFlag: true,
    };
  },

  methods: {
    ...mapMutations("userOption", ["login", "setUser"]),
    checkPhone(str) {
      const mPattern = /^\d{11}$/;
      this.mFlag = mPattern.test(str);
    },
    checkPassword(str) {
      const pPattern = /^\w{6,12}$/;
      this.pFlag = pPattern.test(str);
    },
    async userLogin(userInfo) {
      const resLogin = await userLogin(userInfo);
      sessionStorage.setItem("token", resLogin.data.result.token);
      sessionStorage.setItem("username", resLogin.data.result.name);
      this.userInfo = resLogin.data.result;
      sessionStorage.setItem("user", JSON.stringify(this.userInfo));
      this.login();
      if (resLogin.status == 200) {
        this.loading = false;
        this.$router.push({ name: "home" });
        this.$message({
          message: "恭喜你，成功登陆",
          type: "success",
          duration: 800,
        });
      } else if (resLogin.status == 409) {
        this.loading = false;
        this.$message({
          message: "登陆失败，请检查号码和密码是否正确",
          type: "warning",
          duration: 800,
        });
      }
    },
    goRigister() {
      this.$router.push({ name: "register" });
    },
  },
};
</script>

<style lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  .container {
    margin: 200px;
    width: 500px;
    height: 300px;
    box-shadow: 0 5px 10px lightgray;
    .title {
      width: 100%;
      height: 50px;
      background-color: #2f333a;
      text-align: center;
      line-height: 50px;
      font-size: 16px;
      color: white;
    }
    .con {
      width: 300px;
      height: 100%;
      margin: 0 auto;
      .userInfo {
        position: relative;
        display: flex;
        align-items: center;
        margin: 30px 0;
        input {
          width: 200px;
          height: 30px;
          background-color: white;
        }
        span {
          margin-right: 30px;
          font-size: 16px;
        }
        .checkMassege {
          position: absolute;
          left: 80px;
          bottom: -100%;
          transform: translate(0, -100%);
          color: red;
        }
      }
      .operate {
        display: flex;
        justify-content: space-evenly;
        button {
          padding: 0.8em 1.8em;
          border: 2px solid #17c3b2;
          position: relative;
          overflow: hidden;
          background-color: transparent;
          text-align: center;
          text-transform: uppercase;
          font-size: 12px;
          transition: 0.3s;
          z-index: 1;
          font-family: inherit;
          color: #17c3b2;
        }

        button::before {
          content: "";
          width: 0;
          height: 300%;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%) rotate(45deg);
          background: #17c3b2;
          transition: 0.5s ease;
          display: block;
          z-index: -1;
        }

        button:hover::before {
          width: 105%;
        }

        button:hover {
          color: #111;
        }
      }
    }
  }
}
</style>
